/**
 * @Project TMHSC
 * @Author Tran Manh Hung
 * @Email: hungtmit@gmail.com | tmh@xwebgate.com
 * @Copyright © 2011 TMH. All rights reserved
 * @Createdate Feb 10, 2011
 * @Createtime 4:38:07 PM
 */

package com.tmh.study.fMVW
{

	public class WalkingAvatar extends Avatar
	{
		private var _walkingSpeed:Number = 4;

		private var _isWalking:Boolean = false;

		private var _destX:Number;

		private var _destY:Number;

		public var walkPath:Array;

		public var isoPosition:Point;

		public var world:World;

		public function WalkingAvatar()
		{
			super();
		}

		public function walkByPath():void
		{
			if (walkPath != null && walkPath.length > 0)
			{
				var nextPathSegment:Object = walkPath.pop();
				var nextDeltaX:int = nextPathSegment.x - isoPosition.x;
				var nextDeltaY:int = nextPathSegment.y - isoPosition.y;
				walkTo(nextDeltaX, nextDeltaY);
			}
		}

		public function startWalkByPath():void
		{
			walkByPath();
		}

		public function walkTo(deltaX:int, deltaY:int):void
		{
			/* get the destinetion point from the delta*/
			var destIsoX:int = isoPosition.x + deltaX;
			var destIsoY:int = isoPosition.y + deltaY;
			/* calculate the screen point of the destination from isometric */
			var screenPoint:Point = world.map.i2sLocal(destIsoX, destIsoY);
			var newDirection:String = getDirectionFromDestination(deltaX, deltaY);
			_destX = screenPoint.x;
			_destY = screenPoint.y;
			this.showDirection(newDirection);

			/* we only need to set the animation to walk again when not
			walking or change direciton */
			if (!_isWalking || _currentDirection != newDirection)
			{
				this.playAnimation('walk');
				_isWalking = true;
				_currentDirection = newDirection;
			}
		}

		public function walking():void
		{
			var remainX:int = _destX - this.x;
			var remainY:int = _destY - this.y;
			var remainLenth:Number = Math.sqrt(remainX * remainX + remainY * remainY);

			if (remainLenth > _walkingSpeed)
			{
				this.x += remainX / remainLenth * _walkingSpeed;
				this.y += remainY / remainLenth * _walkingSpeed;
			}
			else
			{
				this.x = _destX;
				this.y = _destY;

				/* Test if this is the end of the path segment or the end of
				the whole path */
				if (walkPath != null && walkPath.length > 0)
				{
					walkByPath();
				}
				else
				{
					stopWalk();
				}
			}
			/* update the isometric coordinate when walking */
			isoPosition = world.map.s2iLocal(this.x, this.y);
		}

		public function stopWalk():void
		{
			this.playAnimation("idle");
			_isWalking = false;
		}

		private function getDirectionFromDestination(deltaX:int, deltaY:int):String
		{
			if (deltaX > 0 && deltaY == 0)
			{
				return "se";
			}
			else if (deltaX < 0 && deltaY == 0)
			{
				return "nw";
			}
			else if (deltaX == 0 && deltaY > 0)
			{
				return "sw";
			}
			else if (deltaX == 0 && deltaY < 0)
			{
				return "ne";
			}
			return _currentDirection;
		}
	}
}